﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace AgLux
{
    /// <summary>
    /// //
    /// </summary>

    public class Picture : Component
    {
        protected Image _picture;
        protected Rectangle _haze;

        /// <summary>
        /// //
        /// </summary>

        /// <param name="source">
        /// //
        /// </param>

        /// <param name="filling">
        /// //
        /// </param>

        /// <param name="caption">
        /// //
        /// </param>

        public Picture(string source, Filling filling, string caption)
            : base()
        {
            _picture = (Image)_resource.FindName("picture");
            _haze = (Rectangle)_resource.FindName("haze");

            _filling = (double)filling / 100;
            _proportion = 0.6;

            _screen.Background = new SolidColorBrush(Colors.Black);
            _screen.MouseMove += new MouseEventHandler(ShowControl);
            _screen.MouseLeave += new EventHandler(HideControl);

            _picture.Source = new Uri(source, UriKind.RelativeOrAbsolute);
            _picture.Visibility = Visibility.Visible;

            _control.Width = _control.Height = 40;
            _control.MouseEnter += new MouseEventHandler(FocusControl);
            _control.MouseLeave += new EventHandler(FocusControl);

            _sizeFull.SetValue(Canvas.LeftProperty, 10);
            _sizeFull.SetValue(Canvas.TopProperty, 10);
            _sizeFull.Visibility = Visibility.Visible;

            _sizeStandard.SetValue(Canvas.LeftProperty, 10);
            _sizeStandard.SetValue(Canvas.TopProperty, 10);

            _caption.Text = caption;
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="canvasWidth">
        /// //
        /// </param>

        public override void FitCanvas(double canvasWidth)
        {
            base.FitCanvas(canvasWidth);

            SetControl();
            SetCaption();

            Height += _caption.Height;
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="value">
        /// //
        /// </param>

        protected override double ScreenWidth
        {
            set
            {
                _screen.Width = _picture.Width = _haze.Width = value;
            }
        }
        /// <summary>
        /// //
        /// </summary>

        /// <param name="value">
        /// //
        /// </param>

        protected override double ScreenHeight
        {
            set
            {
                _screen.Height = _picture.Height = _haze.Height = value;
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sender">
        /// //
        /// </param>

        /// <param name="args">
        /// //
        /// </param>

        protected override void SizeStandard(object sender, MouseEventArgs args)
        {
            base.SizeStandard(sender, args);

            _picture.SetValue(Canvas.LeftProperty, 0);
            _picture.SetValue(Canvas.TopProperty, 0);

            SetControl();
            _caption.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sender">
        /// //
        /// </param>

        /// <param name="args">
        /// //
        /// </param>

        protected override void SizeFull(object sender, MouseEventArgs args)
        {
            base.SizeFull(sender, args);

            if (ScreenWidth * _proportion < ScreenHeight)
            {
                _picture.Height = _picture.Width * _proportion;
                _picture.SetValue(Canvas.TopProperty, (ScreenHeight - _picture.Height) / 2);
            }

            else if (ScreenWidth > ScreenHeight / _proportion)
            {
                _picture.Width = _picture.Height / _proportion;
                _picture.SetValue(Canvas.LeftProperty, (ScreenWidth - _picture.Width) / 2);
            }

            SetControl();
            _caption.Visibility = Visibility.Collapsed;
        }
    }
}
